package com.open.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.system.domain.LoanFeedbackDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanFeedbackService;
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;

/**
 * 反馈内容
 */
 
@Controller
@RequestMapping("/system/loanFeedback")
public class LoanFeedbackController extends BaseController {
	@Autowired
	private LoanFeedbackService loanFeedbackService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanFeedback:loanFeedback")
	String LoanFeedback(){
	    return "system/loanFeedback/loanFeedback";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanFeedback:loanFeedback")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="意见反馈";
		String detail="查看了已经反馈信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		//查询列表数据
        Query query = new Query(params);
		List<LoanFeedbackDO> loanFeedbackList = loanFeedbackService.list(query);
		int total = loanFeedbackService.count(query);
		PageUtils pageUtils = new PageUtils(loanFeedbackList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanFeedback:add")
	String add(){
	    return "system/loanFeedback/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanFeedback:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanFeedbackDO loanFeedback = loanFeedbackService.get(id);
		model.addAttribute("loanFeedback", loanFeedback);
	    return "system/loanFeedback/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanFeedback:add")
	public R save( LoanFeedbackDO loanFeedback){
		loanFeedback.setCreateTime(new Date());
		if(loanFeedbackService.save(loanFeedback)>0){
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="意见反馈";
			String detail="新增了反馈信息";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanFeedback:edit")
	public R update( LoanFeedbackDO loanFeedback){
		if(loanFeedback.getRemark3()==null || loanFeedback.getRemark3()==""){
			return R.error(203,"官方回复不可以为空");
		}
		loanFeedback.setStatus("1");
		loanFeedback.setDealwithTime(new Date());
		loanFeedbackService.update(loanFeedback);
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="意见反馈";
		String detail="处理了反馈信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanFeedback:remove")
	public R remove( Long id){
		if(loanFeedbackService.remove(id)>0){
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="意见反馈";
			String detail="删除了【"+id+"】反馈信息";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanFeedback:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanFeedbackService.batchRemove(ids);
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="意见反馈";
		String detail="批量删除了【"+ids+"】反馈信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		return R.ok();
	}
	
}
